package com.zking.web.ly;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.zking.dao.domain.Post;
import com.zking.service.ly.PositionsService;

@Controller
public class PositionsController {
	@Autowired
	PositionsService pss;

//	@Autowired
//	private RedisUtil redisUtil;
//
//	// 查询集合（有效职位）
//	@RequestMapping("/see")
//	public String see(Post ps) {
//		List<Post> lpt = pss.sees(ps);
//		if (redisUtil.lGetListSize("lpt") == 0) {
//			for (Post post : lpt) {
//				redisUtil.lSet("lpt", JSONObject.toJSONString(post), 120);
//			}
//			return "redirect:/sees";
//		} else {
//			return "redirect:/sees";
//		}
//	}
//
//	// 分页查询
//	@RequestMapping("/sees")
//	public String sees(HttpServletRequest request) {
//		String pnS = request.getParameter("pn");
//
//		int pn = 0;
//		int limit = 4;
//		try {
//			pn = Integer.parseInt(pnS);
//		} catch (Exception e) {
//			pn = 1;
//		}
//
//		if (pn < 1) {
//			pn = 1;
//		}
//
//		int total = (int) redisUtil.lGetListSize("lpt");
//		int pages = total / limit;
//		if (total % limit != 0) {
//			pages++;
//		}
//
//		if (pn > pages) {
//			pn = pages;
//		}
//
//		int start = (pn - 1) * limit;// 第一个参数
//		limit = start + 4;
//		List<Object> list = redisUtil.lGet("lpt", start, limit - 1);
//		List<Post> lpts = new ArrayList<Post>();
//		for (Object object : list) {
//			lpts.add(JSONObject.parseObject(object.toString(), Post.class));
//		}
//		if (!lpts.isEmpty()) {
//			request.setAttribute("pts", lpts);
//			request.setAttribute("pn", pn);
//			request.setAttribute("limit", limit);
//			request.setAttribute("pages", pages);
//			return "positions";
//		} else {
//			request.setAttribute("msg", "没有查询到数据");
//			return "positions";
//		}
//	}
//
//	// 查询集合（已下线职位）
//	@RequestMapping("/see2")
//	public String see2(Post ps) {
//		List<Post> lpt2 = pss.sees(ps);
//		if (redisUtil.lGetListSize("lpt2") == 0) {
//			for (Post post : lpt2) {
//				redisUtil.lSet("lpt2", JSONObject.toJSONString(post), 10);
//			}
//			return "redirect:/see2s";
//		} else {
//			return "redirect:/see2s";
//		}
//	}
//
//	// 分页查询
//	@RequestMapping("/see2s")
//	public String see2s(HttpServletRequest request) {
//		String pnS = request.getParameter("pn");
//
//		int pn = 0;
//		int limit = 4;
//		try {
//			pn = Integer.parseInt(pnS);
//		} catch (Exception e) {
//			pn = 1;
//		}
//
//		if (pn < 1) {
//			pn = 1;
//		}
//
//		int total = (int) redisUtil.lGetListSize("lpt2");
//		int pages = total / limit;
//		if (total % limit != 0) {
//			pages++;
//		}
//
//		if (pn > pages) {
//			pn = pages;
//		}
//
//		int start = (pn - 1) * limit;// 第一个参数
//		limit = start + 4;
//		List<Object> list = redisUtil.lGet("lpt2", start, limit - 1);
//		List<Post> lpts2 = new ArrayList<Post>();
//		for (Object object : list) {
//			lpts2.add(JSONObject.parseObject(object.toString(), Post.class));
//		}
//		if (!lpts2.isEmpty()) {
//			request.setAttribute("ptss", lpts2);
//			request.setAttribute("pn", pn);
//			request.setAttribute("limit", limit);
//			request.setAttribute("pages", pages);
//			return "positions2";
//		} else {
//			request.setAttribute("msg", "没有查询到数据");
//			return "positions2";
//		}
//	}
	// 查询集合（有效职位）
	@RequestMapping(value = "/see")
	public ModelAndView see(Post ps, HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView();
		List<Post> lpt = pss.sees(ps);
		System.out.println("lpt：" + lpt);
		if (lpt != null && lpt.size() != 0) {
			System.out.println("进来了");
			modelAndView.addObject("pts", lpt);
			modelAndView.setViewName("positions");
		} else {
			request.setAttribute("msg", "没查询到数据！");
			modelAndView.setViewName("positions");
		}
		return modelAndView;
	}

	// 查询集合（已下线职位）
	@RequestMapping(value = "/see2")
	public ModelAndView see2(Post ps, HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView();
		List<Post> lpt2 = pss.sees(ps);
		System.out.println("lpt2：" + lpt2);
		if (lpt2 != null && lpt2.size() != 0) {
			System.out.println("进来了");
			modelAndView.addObject("ptss", lpt2);
			modelAndView.setViewName("positions2");
		} else {
			System.out.println("=======");
			request.setAttribute("msg", "没查询到数据！");
			modelAndView.setViewName("positions2");
		}
		return modelAndView;
	}

	// 编辑职位
	@RequestMapping(value = "/uply")
	public ModelAndView update1(Integer id, Integer information_id, HttpServletRequest request,
			@ModelAttribute Post ps) {
		System.out.println("id=" + id);
		System.out.println("information_id=" + information_id);
		ModelAndView modelAndView = new ModelAndView("redirect:/see?id=" + id + "&information_id=" + information_id);
		Post s = pss.see(id);
		if (s != null) {
			s.setJob_type(ps.getJob_type());
			s.setJob_name(ps.getJob_name());
			s.setDepartment(ps.getDepartment());
			s.setJob_nature(ps.getJob_nature());
			s.setJob_malary(ps.getJob_malary());
			s.setJob_salary(ps.getJob_salary());
			s.setJob_city(ps.getJob_city());
			s.setJob_experience(ps.getJob_experience());
			s.setJob_academic(ps.getJob_academic());
			s.setJob_welfare(ps.getJob_welfare());
			s.setJob_describe(ps.getJob_describe());
			s.setJob_address(ps.getJob_address());
			s.setCompany_email(ps.getCompany_email());
			System.out.println("s=" + s);
			int i = pss.up(s);
			System.out.println("i" + i);
			modelAndView.addObject("is", i);
			return modelAndView;
		} else {
			System.out.println("修改失败！");
		}
		return modelAndView;
	}

	// 查询一条数据
	@RequestMapping(value = "/qu1")
	public ModelAndView query(Integer id, HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView();
		Post ps = pss.see(id);
		if (ps != null) {
			modelAndView.addObject("ps", ps);
			modelAndView.setViewName("create2");
		} else {
			modelAndView.setViewName("查询失败");
		}
		return modelAndView;
	}

	//
	@RequestMapping(value = "/qu2")
	public ModelAndView query2(Integer id, HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView();
		Post ps = pss.see(id);
		if (ps != null) {
			modelAndView.addObject("ps", ps);
			modelAndView.setViewName("create3");
		} else {
			modelAndView.setViewName("查询失败");
		}
		return modelAndView;
	}

	// 下线职位
	@RequestMapping(value = "/uply2")
	public ModelAndView update2(Integer id, Integer information_id, HttpServletRequest request,
			@ModelAttribute Post ps) {
		System.out.println("id=" + id);
		System.out.println("information_id=" + information_id);
		ModelAndView modelAndView = new ModelAndView("redirect:/see?id=" + id + "&information_id=" + information_id);
		Post s = pss.see(id);
		if (s != null) {
			System.out.println("Job_status:" + s.getJob_status());
			if (s.getJob_status().equals("有效")) {
				s.setJob_status("无效");
				int i = pss.up(s);
				System.out.println("Job_status:" + s.getJob_status());
				modelAndView.addObject("is", i);
			} else if (s.getJob_status().equals("无效")) {
				s.setJob_status("有效");
				int i = pss.up(s);
				modelAndView.addObject("is", i);
			}
			return modelAndView;
		} else {
			System.out.println("下线失败！");
		}
		return modelAndView;
	}

	// 删除
	@RequestMapping(value = "/del")
	public ModelAndView delete(Integer id, HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView();
		int del = pss.de(id);
		modelAndView.addObject(del);
		modelAndView.setViewName("create");
		return modelAndView;
	}
}
